﻿namespace FluentMvc.Validations
{
    using System.Collections.Generic;
    using System.Web.Mvc;

    public class RequiredValidator : FluentValidator, IClientValidatable
    {
        public override bool Validate(FluentValidationContext context)
        {
            if (context.Value is string && string.IsNullOrEmpty((string)context.Value))
                return false;
            return context.Value != null;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            return new List<ModelClientValidationRule>
                       {
                           new ModelClientValidationRule
                               {
                                   ValidationType = "required",
                                   ErrorMessage = ErrorMessage
                               }
                       };
        }
    }
}